package tcpSocket;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import client.SystemProperties;

import net.sf.json.JSONObject;

/**
 * 
 * @author David de Hoop
 */
public class GUISocket {
    
	// Puts sensorboundary values for analogSensors into a JSONObject.
    public String SensorBoundary(String sensorname, double lowerboundary, double upperboundary) {
        JSONObject SensorBoundaryValues = new JSONObject();
        
	//Put values into JSONObject.	 
	SensorBoundaryValues.put("sensorname", sensorname);
	SensorBoundaryValues.put("lowerboundary", lowerboundary);
	SensorBoundaryValues.put("upperboundary", upperboundary);
        
		//Put all values into JSONObject with alarmName
        JSONObject SensorBoundaries = new JSONObject();
        SensorBoundaries.put("analogAlarmChange", SensorBoundaryValues);
        
		// Format toString() then return as a String.
        String JSONString = SensorBoundaries.toString();
                
        return JSONString;
    }
    
	// Puts sensorboundary values for binarySensors into a JSONObject
    public String SensorBoundaryBinary(String sensorname, boolean alarmstatus) {
        JSONObject SensorBoundaryValues = new JSONObject();
     
	// Put values into JSONObject.
	SensorBoundaryValues.put("sensorname", sensorname);
	SensorBoundaryValues.put("alarm", alarmstatus);
	
		// Put all values into JSONObjecr with alarmName.
        JSONObject SensorBoundaries = new JSONObject();
        SensorBoundaries.put("binaryAlarmChange", SensorBoundaryValues);
        
		// Format toString() then return as a String.
        String JSONString = SensorBoundaries.toString();
                
        return JSONString;
    }
    
	//Opens a TCP connection to the server to send JSONObjects.
    public void TCPSocket(String JSONObject) throws IOException {
    	String ipAddress = SystemProperties.getServerIp();
    	int portnumber = Integer.parseInt(SystemProperties.getServerPort());
        Socket GUISocket = new Socket(ipAddress,portnumber);
        
		// Open an stream to send the data.
        DataOutputStream ToServer = new DataOutputStream(GUISocket.getOutputStream());
        
		// Write the data then close the inputstream and then close the socket.
		ToServer.writeBytes(JSONObject);
        ToServer.close();
        GUISocket.close();
    }
    
     /**
     * @param args the command line arguments
     */

}
